#!/usr/bin/env python3
"""
视频尺寸调整组件

包含尺寸调整功能：
1. 尺寸调整方式选择下拉列表
2. 常见分辨率选择
3. 等比缩放滑块
4. 自定义分辨率输入
"""

from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, 
                             QLineEdit, QPushButton, QMessageBox, QDialog,
                             QComboBox, QSlider, QDialogButtonBox)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont, QIntValidator


class CommonResolutionDialog(QDialog):
    """常见分辨率选择对话框"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.selected_resolution = "1920×1080"
        self.init_ui()
    
    def init_ui(self):
        """初始化UI"""
        self.setWindowTitle("选择常见分辨率")
        self.setFixedSize(300, 200)
        
        layout = QVBoxLayout()
        
        # 标题
        title_label = QLabel("📏 选择视频分辨率")
        title_label.setFont(QFont("Microsoft YaHei", 12, QFont.Weight.Bold))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("color: #2c3e50; margin-bottom: 15px;")
        layout.addWidget(title_label)
        
        # 分辨率下拉框
        self.resolution_combo = QComboBox()
        
        # 常见分辨率列表
        resolutions = [
            "3840×2160 (4K UHD)",
            "4096×2160 (4K DCI)",
            "2560×1440 (2K QHD)",
            "1920×1080 (1080P FHD)",
            "1280×720 (720P HD)",
            "854×480 (480P SD)",
            "640×360 (360P)",
            "426×240 (240P)"
        ]
        self.resolution_combo.addItems(resolutions)
        self.resolution_combo.setCurrentText("1920×1080 (1080P FHD)")
        self.resolution_combo.setStyleSheet("""
            QComboBox {
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                padding: 8px;
                background-color: #2c3e50;
                color: #ecf0f1;
                font-family: 'Microsoft YaHei', Arial, sans-serif;
                font-size: 11px;
            }
            QComboBox:focus {
                border-color: #3498db;
                background-color: #34495e;
            }
        """)
        layout.addWidget(self.resolution_combo)
        
        # 按钮
        button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
        button_box.accepted.connect(self.accept_selection)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)
        
        self.setLayout(layout)
    
    def accept_selection(self):
        """接受选择"""
        resolution_text = self.resolution_combo.currentText()
        # 提取分辨率数值（如"1920×1080"）
        self.selected_resolution = resolution_text.split(" (")[0]
        self.accept()


class ScaleDialog(QDialog):
    """等比缩放对话框"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.scale_percentage = 100
        self.init_ui()
    
    def init_ui(self):
        """初始化UI"""
        self.setWindowTitle("等比缩放设置")
        self.setFixedSize(350, 180)
        
        layout = QVBoxLayout()
        
        # 标题
        title_label = QLabel("⚖️ 等比缩放设置")
        title_label.setFont(QFont("Microsoft YaHei", 12, QFont.Weight.Bold))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("color: #2c3e50; margin-bottom: 15px;")
        layout.addWidget(title_label)
        
        # 缩放百分比显示
        self.scale_label = QLabel(f"缩放比例: {self.scale_percentage}%")
        self.scale_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.scale_label.setStyleSheet("font-family: 'Microsoft YaHei', Arial, sans-serif; font-size: 14px; font-weight: bold; color: #3498db;")
        layout.addWidget(self.scale_label)
        
        # 滑块
        self.scale_slider = QSlider(Qt.Orientation.Horizontal)
        self.scale_slider.setMinimum(10)
        self.scale_slider.setMaximum(200)
        self.scale_slider.setValue(100)
        self.scale_slider.setTickPosition(QSlider.TickPosition.TicksBelow)
        self.scale_slider.setTickInterval(10)
        self.scale_slider.valueChanged.connect(self.update_scale_label)
        self.scale_slider.setStyleSheet("""
            QSlider::groove:horizontal {
                border: 1px solid #bdc3c7;
                height: 8px;
                background: #ecf0f1;
                border-radius: 4px;
            }
            QSlider::handle:horizontal {
                background: #3498db;
                border: 2px solid #2980b9;
                width: 18px;
                margin: -8px 0;
                border-radius: 9px;
            }
            QSlider::handle:horizontal:hover {
                background: #2980b9;
            }
        """)
        layout.addWidget(self.scale_slider)
        
        # 刻度标签
        scale_info = QLabel("10% ────────────────────────────────────────────────── 200%")
        scale_info.setAlignment(Qt.AlignmentFlag.AlignCenter)
        scale_info.setStyleSheet("font-family: 'Microsoft YaHei', Arial, sans-serif; font-size: 10px; color: #7f8c8d;")
        layout.addWidget(scale_info)
        
        # 按钮
        button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)
        
        self.setLayout(layout)
    
    def update_scale_label(self, value):
        """更新缩放标签"""
        self.scale_percentage = value
        self.scale_label.setText(f"缩放比例: {value}%")


class CustomResolutionDialog(QDialog):
    """自定义分辨率对话框"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.custom_width = 1920
        self.custom_height = 1080
        self.init_ui()
    
    def init_ui(self):
        """初始化UI"""
        self.setWindowTitle("自定义分辨率")
        self.setFixedSize(350, 200)
        
        layout = QVBoxLayout()
        
        # 标题
        title_label = QLabel("🎯 自定义分辨率")
        title_label.setFont(QFont("Microsoft YaHei", 12, QFont.Weight.Bold))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("color: #2c3e50; margin-bottom: 15px;")
        layout.addWidget(title_label)
        
        # 分辨率输入行
        resolution_layout = QHBoxLayout()
        
        # 宽度输入
        width_layout = QVBoxLayout()
        width_label = QLabel("宽度:")
        width_label.setStyleSheet("font-family: 'Microsoft YaHei', Arial, sans-serif; font-size: 11px;")
        width_layout.addWidget(width_label)
        
        self.width_input = QLineEdit("1920")
        self.width_input.setValidator(QIntValidator(1, 9999))
        self.width_input.setStyleSheet("""
            QLineEdit {
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                padding: 6px;
                background-color: #2c3e50;
                color: #ecf0f1;
                font-family: 'Microsoft YaHei', Arial, sans-serif;
                font-size: 11px;
                max-width: 80px;
            }
        """)
        width_layout.addWidget(self.width_input)
        resolution_layout.addLayout(width_layout)
        
        # 分隔符
        separator = QLabel("×")
        separator.setAlignment(Qt.AlignmentFlag.AlignCenter)
        separator.setStyleSheet("font-family: 'Microsoft YaHei', Arial, sans-serif; font-size: 14px; font-weight: bold; color: #2c3e50; margin: 0 10px;")
        resolution_layout.addWidget(separator)
        
        # 高度输入
        height_layout = QVBoxLayout()
        height_label = QLabel("高度:")
        height_label.setStyleSheet("font-family: 'Microsoft YaHei', Arial, sans-serif; font-size: 11px;")
        height_layout.addWidget(height_label)
        
        self.height_input = QLineEdit("1080")
        self.height_input.setValidator(QIntValidator(1, 9999))
        self.height_input.setStyleSheet("""
            QLineEdit {
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                padding: 6px;
                background-color: #2c3e50;
                color: #ecf0f1;
                font-family: 'Microsoft YaHei', Arial, sans-serif;
                font-size: 11px;
                max-width: 80px;
            }
        """)
        height_layout.addWidget(self.height_input)
        resolution_layout.addLayout(height_layout)
        
        # 单位
        unit_label = QLabel("px")
        unit_label.setStyleSheet("font-family: 'Microsoft YaHei', Arial, sans-serif; font-size: 11px; margin-left: 5px;")
        resolution_layout.addWidget(unit_label)
        
        resolution_layout.addStretch()
        layout.addLayout(resolution_layout)
        
        # 按钮
        button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
        button_box.accepted.connect(self.accept_custom)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)
        
        self.setLayout(layout)
    
    def accept_custom(self):
        """接受自定义分辨率"""
        if not self.width_input.text() or not self.height_input.text():
            QMessageBox.warning(self, "警告", "请输入宽度和高度！")
            return
        
        self.custom_width = int(self.width_input.text())
        self.custom_height = int(self.height_input.text())
        
        if self.custom_width < 1 or self.custom_height < 1:
            QMessageBox.warning(self, "警告", "分辨率必须大于0！")
            return
        
        self.accept()


class VideoSizeWidget(QWidget):
    """视频尺寸调整组件"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.size_option = "保持原视频分辨率"
        self.size_value = "保持原分辨率"
        self.init_ui()
    
    def init_ui(self):
        """初始化UI"""
        # 主布局
        main_layout = QHBoxLayout()
        
        # 尺寸标签
        size_label = QLabel("尺寸：")
        size_label.setFont(QFont("Microsoft YaHei", 10, QFont.Weight.Bold))
        size_label.setStyleSheet("color: #2c3e50; min-width: 40px;")
        main_layout.addWidget(size_label)
        
        # 尺寸选项下拉框
        self.size_combo = QComboBox()
        size_options = [
            "保持原视频分辨率",
            "常见分辨率",
            "等比缩放",
            "自定义"
        ]
        self.size_combo.addItems(size_options)
        self.size_combo.setCurrentText("保持原视频分辨率")
        self.size_combo.setStyleSheet("""
            QComboBox {
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                padding: 6px;
                background-color: #2c3e50;
                color: #ecf0f1;
                font-family: 'Microsoft YaHei', Arial, sans-serif;
                font-size: 11px;
                min-width: 140px;
            }
            QComboBox:focus {
                border-color: #3498db;
                background-color: #34495e;
            }
        """)
        self.size_combo.currentTextChanged.connect(self.on_size_option_changed)
        main_layout.addWidget(self.size_combo)
        
        # 显示结果标签
        self.result_label = QLabel("保持原分辨率")
        self.result_label.setStyleSheet("""
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            font-size: 11px;
            color: #ffffff;
            padding: 6px 12px;
            background-color: #27ae60;
            border-radius: 4px;
            border: 1px solid #229954;
            font-weight: bold;
            margin-left: 10px;
        """)
        main_layout.addWidget(self.result_label)
        
        main_layout.addStretch()
        self.setLayout(main_layout)
    
    def on_size_option_changed(self, option):
        """尺寸选项改变事件"""
        self.size_option = option
        
        if option == "保持原视频分辨率":
            self.size_value = "保持原分辨率"
            self.result_label.setText("保持原分辨率")
            self.result_label.setStyleSheet(self.get_result_style("#27ae60"))
            
        elif option == "常见分辨率":
            dialog = CommonResolutionDialog(self)
            if dialog.exec() == QDialog.DialogCode.Accepted:
                self.size_value = dialog.selected_resolution
                self.result_label.setText(self.size_value)
                self.result_label.setStyleSheet(self.get_result_style("#3498db"))
            else:
                # 如果取消，恢复原选项
                self.size_combo.setCurrentText("保持原视频分辨率")
                
        elif option == "等比缩放":
            dialog = ScaleDialog(self)
            if dialog.exec() == QDialog.DialogCode.Accepted:
                self.size_value = f"缩放{dialog.scale_percentage}%"
                self.result_label.setText(self.size_value)
                self.result_label.setStyleSheet(self.get_result_style("#e67e22"))
            else:
                self.size_combo.setCurrentText("保持原视频分辨率")
                
        elif option == "自定义":
            dialog = CustomResolutionDialog(self)
            if dialog.exec() == QDialog.DialogCode.Accepted:
                self.size_value = f"{dialog.custom_width}×{dialog.custom_height}"
                self.result_label.setText(self.size_value)
                self.result_label.setStyleSheet(self.get_result_style("#9b59b6"))
            else:
                self.size_combo.setCurrentText("保持原视频分辨率")
    
    def get_result_style(self, color):
        """获取结果显示样式"""
        return f"""
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            font-size: 11px;
            color: #ffffff;
            padding: 6px 12px;
            background-color: {color};
            border-radius: 4px;
            border: 1px solid {self.darken_color(color)};
            font-weight: bold;
            margin-left: 10px;
        """
    
    def darken_color(self, color):
        """加深颜色"""
        # 简单的颜色加深逻辑
        color_map = {
            "#27ae60": "#229954",  # 绿色
            "#3498db": "#2980b9",  # 蓝色
            "#e67e22": "#d35400",  # 橙色
            "#9b59b6": "#8e44ad"   # 紫色
        }
        return color_map.get(color, color)
    
    def get_size_settings(self):
        """获取尺寸设置"""
        return {
            "option": self.size_option,
            "value": self.size_value
        }


if __name__ == "__main__":
    # 测试代码
    import sys
    from PyQt6.QtWidgets import QApplication
    
    app = QApplication(sys.argv)
    
    # 测试尺寸组件
    size_widget = VideoSizeWidget()
    size_widget.show()
    
    sys.exit(app.exec())